<?php
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Controller;
// use app\common\validate\Sector as SectorVa;
class Admin extends Controller
{
    public function _initialize()
    {
        if(!Session::has('user_name')){
            return $this->error('非法登陆!');
        }
    }

    public function index()
    {      
        $my=model('Sector');
        $sectorName=$my->getSectorName(0);
        $sectorname=$my->getSectorName(3);
        $downname=$my->where('fathersector',4)->select();
        return view('index',[
            'sectorName'=>$sectorName,
            'sectorname'=>$sectorname,
            'downname'=>$downname
        ]);
       
    }

    public function addSector()
    {
        return view('addsector',['fid'=>3]);
    }

    public function adddown()
    {
        return view('addsector',['fid'=>4]);
    }
    
    public function doAddSector()
    {
        $res=request();
        $ress=$res->get('fid');
        if(!is_numeric($ress))return '非法入侵！';
        $res=$res->post();
        $va=validate('SectorVa');
        $my=model('Sector');
        if($my->where('fathersector',$ress)->find())
        {
            foreach ($res['sectors'] as $key => $value) 
            {
                if(!$va->check(['sectorname'=>$value]))
                {
                    return $va->getError();
                }else{
                    if(!$my->insert(['sectorname'=>$value,'fathersector'=>$ress]))return '添加失败！';
                }
            }
            return '添加成功！';
        }else{
            return '非法入侵!';
        }
    }

    public function showSectorName()
    {
        $res=request();
        if ($res->isGet())
        {
            $res=$res->get();
            $my=model('Sector');
            $sectorName=$my->getSectorName(0);
            return view('show',['sectname'=>$sectorName,'name'=>$res['name']]); 
        }
        
    }

    public function updateSectorName()
    {
        $res=request();
        if ($res->isPost())
        {
            $my=model('Sector');
            $res=$res->post();
            foreach ($res['id'] as $key => $value) {
                $my->save(['sectorname'=>$res['sitename'][$key]],['sid'=>$value]);
            }
            Header("Location: /nav/admin/ssn.html?name=修改左侧栏名称");
        }
    }

    public function showUrl()
    {
        $res=request();
        $lid=$res->get('lid');
        
        if($res->get('fa')==NULL)
        {
            $res=Db::name('sector')->alias('a')->join('__SITE__ b ','b.sid= a.sid')->where('a.sid',$lid)->select();      
            return view('showurl',['datas'=>$res,'sid'=>$lid]);
        }else
        {
            $my=model('Arts');
            $res=Db::name('sector')->alias('a')->join('__ARTS__ b ','b.sid= a.sid')
                    ->where('a.sid',$lid)->order('top desc,rid desc')
                    ->paginate(18,false,['path'=>'/nav/admin/usu.html',
                    'query'=>['fa'=>1,'lid'=>$lid],
                    ]);      
          
            return view('showdown',['sid'=>$lid,'res'=>$res]);
            // 未完再续
        }
    }

    public function changeSite()
    {
        $res=request();
        
        if($res->get('me'))
        {
            if($res->get('me')==1)
            {
                
                foreach ($res->post()['sitename'] as $key => $value) 
                {
                    model('site')->save(
                        ['webname'=>$res->post()['sitename'][$key],'website'=>$res->post()['site'][$key]],
                        ['aid'=>$res->post()['aid'][$key]]
                    );
                }
            }elseif($res->get('me')==2)
            {
                
                foreach ($res->post()['sitename'] as $key => $value) 
                {
                    
                    model('site')->insert(['webname'=>$res->post()['sitename'][$key],
                    'website'=>$res->post()['site'][$key],
                    'sid'=>$res->get('sid')]);
                }   
            }    
        }else
        {
            return '非法链接';
        }
        Header("Location: /nav/admin/usu?lid=".$res->get('sid')); 
        
    }

    public function up()
    {
        date_default_timezone_set("Asia/chongqing");
        error_reporting(E_ERROR);
        header("Content-Type: text/html; charset=utf-8");

        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(PPP."static/js/ueditor/config.json")), true);
        $action = $_GET['action'];

        switch ($action) 
        {
            case 'config':
                $result =  json_encode($CONFIG);
                break;

            /* 上传图片 */
            case 'uploadimage':
            /* 上传涂鸦 */
            case 'uploadscrawl':
            /* 上传视频 */
            case 'uploadvideo':
            /* 上传文件 */
            case 'uploadfile':
                // $result = include("action_upload.php");
                $result = include(APP_PATH.'/../extend/uditor/action_upload.php');
                
                break;

            /* 列出图片 */
            case 'listimage':
                $result = include(APP_PATH.'/../extend/uditor/action_list.php');
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include(APP_PATH.'/../extend/uditor/action_list.php');
                break;

            /* 抓取远程文件 */
            case 'catchimage':
                $result = include(APP_PATH.'/../extend/uditor/action_crawler.php');
                break;

            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                return json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            return $result;
        }
    }

    
    public function addart()
    {
        $secs=model('Sector')->getSectorName(4);
        return view('addart',['sid'=>request()->get('sid'),'sec'=>$secs]);
    }

    public function saveart()
    {
        $res=request()->post();
        $my=model('arts');
        $my->insert(['arname'=>$res['title'],
        'sid'=>$res['sid'],
        'content'=>$res['editorValue']]);
        header("Location: /nav/admin/usu.html?fa=1&lid=".$res['sid']);
    }

    public function del()
    {
        $rid=request()->get('rid');
        $sid=request()->get('sid');
        Db::name('arts')->where('rid',$rid)->delete();
        header("Location: /nav/admin/usu.html?fa=1&lid=".$sid);
    }

    public function change()
    {
        $rid=request()->get('rid');
        $res=model('Arts')->where('rid',$rid)->find();
        $secs=model('Sector')->getSectorName(4);
        return view('addart1',['res'=>$res,'sec'=>$secs]);
    }

    public function totop()
    {
        $my=model('Arts');
        if(request()->get('top')==1){
            $my->where('rid',request()->get('rid'))->update(['top'=>1]);
            return '置顶成功！';
        }else if(request()->get('top')==0){
            $my->where('rid',request()->get('rid'))->update(['top'=>0]);
            return '取消置顶成功！';
        }
    }
}
